package com.adcc.swingui.conf;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.adcc.swingui.bean.MenuBean;
import com.adcc.swingui.bean.MenuComponentBean;

/**
 * 菜单配置
 * 
 * @author charlie
 * 
 */
public class MenuBarConf {
	private static Logger logger = Logger.getLogger(MenuBarConf.class);
	private static Map<String, MenuBean> menuConf = new HashMap<String, MenuBean>();
	private static List<String> authorityList = null;

	@Deprecated
	public static void initConf(String path) {
		try {
			SAXReader reader = new SAXReader();
			Document doc = reader.read(path);
			Element root = (Element) doc.getRootElement();
			if (!root.getName().equals("root")) {
				logger.error("读取root根节点异常");
			}

			if (root != null) {
				for (Iterator i = root.elementIterator(); i.hasNext();) {
					MenuBean bean = new MenuBean();
					Element menuBar = (Element) i.next();
					// 读取配置文件获取对应的JMenu或者JMenuItem
					List<MenuComponentBean> menuList = readF(menuBar);
					bean.setName(menuBar.attributeValue("name"));
					bean.setI18nName(menuBar.attributeValue("i18nName"));
					bean.setMenuList(menuList);
					menuConf.put(bean.getName(), bean);
				}
			}
		} catch (DocumentException e) {
			logger.error(path + "解析失败严重错误" + path);
		}
	}

	/**
	 * 递归读取xml文件中menu和menuItem数据
	 * 
	 * @param component
	 * @return
	 */
	@Deprecated
	private static List<MenuComponentBean> readF(Element component) {
		List<MenuComponentBean> result = new ArrayList<MenuComponentBean>();
		for (Iterator k = component.elementIterator(); k.hasNext();) {
			Element itemElement = (Element) k.next();
			MenuComponentBean componentBean = new MenuComponentBean();
			componentBean.setName(itemElement.attributeValue("name"));
			componentBean.setI18nName(itemElement.attributeValue("i18nName"));
			componentBean.setMethod(itemElement.attributeValue("method"));
			componentBean.setIcon(itemElement.attributeValue("icon"));
			componentBean.setCompEnabled(!"false".equals(itemElement
					.attributeValue("isEnabled")));
			componentBean.setOffLineVisiable(!"false".equals(itemElement
					.attributeValue("offLineVisiable")));
			Iterator j = itemElement.elementIterator();
			if (j.hasNext()) {
				componentBean.setItemList(readF(itemElement));
			}
			result.add(componentBean);
		}
		return result;
	}

	/**
	 * @Title: initConfNew
	 * @Description: TODO 菜单xml解析
	 * @param @param path
	 * @return void
	 * @throws
	 */
	public static void initConfNew(String path) {
		try {
			SAXReader reader = new SAXReader();
			Document doc = reader.read(path);
			Element root = doc.getRootElement();
			if (!root.getName().equals("root")) {
				logger.error("读取root根节点异常");
			}

			if (root != null) {
				for (Iterator i = root.elementIterator(); i.hasNext();) {
					MenuBean bean = new MenuBean();
					Element menuBar = (Element) i.next();
					bean.setName(menuBar.attributeValue("name"));
					bean.setI18nName(menuBar.attributeValue("i18nName"));
					bean.setI18nName(menuBar.attributeValue("baseIcon"));
					// 读取配置文件获取对应的JMenu或者JMenuItem
					List<MenuComponentBean> menuList = processMenuItem(menuBar,
							null);
					bean.setMenuList(menuList);
					menuConf.put(bean.getName(), bean);
				}
			}
		} catch (DocumentException e) {
			logger.error(path + "解析失败严重错误" + path);
		}
	}

	public static void initConfNew(String path, List<String> privilege) {
		try {
			authorityList = privilege;
			SAXReader reader = new SAXReader();
			Document doc = reader.read(path);
			Element root = doc.getRootElement();
			if (!root.getName().equals("root")) {
				logger.error("读取root根节点异常");
			}

			if (root != null) {
				for (Iterator i = root.elementIterator(); i.hasNext();) {
					MenuBean bean = new MenuBean();
					Element menuBar = (Element) i.next();
					bean.setName(menuBar.attributeValue("name"));
					bean.setI18nName(menuBar.attributeValue("i18nName"));
					bean.setI18nName(menuBar.attributeValue("baseIcon"));
					// 读取配置文件获取对应的JMenu或者JMenuItem
					List<MenuComponentBean> menuList = processMenuItem(menuBar,
							null);
					bean.setMenuList(menuList);
					menuConf.put(bean.getName(), bean);
				}
			}
		} catch (DocumentException e) {
			logger.error(path + "解析失败严重错误" + path);
		}
	}

	/**
	 * 递归一级以下菜单
	 */
	private static List<MenuComponentBean> processMenuItem(Element menuBar,
			MenuComponentBean menuBean) {

		List<MenuComponentBean> result = new ArrayList<MenuComponentBean>();

		for (Iterator menu = menuBar.elementIterator(); menu.hasNext();) {

			Element itemElement = (Element) menu.next();

			boolean isMenuItem = itemElement.getName().equalsIgnoreCase(
					"menuitem");

			boolean isMenu = itemElement.getName().equalsIgnoreCase("menu");

			if (isMenuItem || isMenu) {

				String name = itemElement.attributeValue("name");
				String i18nName = itemElement.attributeValue("i18nName");
				String menuClass = itemElement.attributeValue("menuClass");
				String eventType = itemElement.attributeValue("eventType");
				String icon = itemElement.attributeValue("icon");
				boolean isEnabled = !"false".equals(itemElement
						.attributeValue("isEnabled"));
				boolean offLineVisiable = !"false".equals(itemElement
						.attributeValue("offLineVisiable"));
				String eventAction = itemElement.attributeValue("eventAction");
				String privilege = itemElement.attributeValue("privilegestr");

				if (name == null || name.trim().equals("")) {
					logger.error("请检查menu.xml文件,name属性不能为空！");
					continue;
				}

				if (eventType == null || eventType.trim().equals("")) {
					eventType = "method";
				}

				if (eventType.equals("method")
						&& (eventAction == null || eventAction.trim()
								.equals(""))) {
					logger.error("请检查menu.xml文件," + name
							+ "节点的eventAction属性不能为空！");
					continue;
				}

				if (null == menuClass || "".equals(menuClass.trim())) {
					menuClass = (isMenu ? "javax.swing.JMenu"
							: "javax.swing.JMenuItem");
				}

				if (eventAction.startsWith("%s")) {
					eventAction = eventAction.replaceFirst("%s",
							menuBean.getEventAction());
				}

				MenuComponentBean componentBean = new MenuComponentBean();
				componentBean.setName(name);
				componentBean.setI18nName(i18nName);
				componentBean.setMenuClass(menuClass);
				componentBean.setIcon(icon);
				componentBean.setCompEnabled(isEnabled);
				componentBean.setOffLineVisiable(offLineVisiable);
				componentBean.setEventType(eventType);
				componentBean.setEventAction(eventAction);
				componentBean.setLeaf(isMenuItem);
				componentBean.setPrivilegestr(privilege);
				if (isMenu) {
					componentBean.setItemList(processMenuItem(itemElement,
							componentBean));
				}
				if (haveAuthority(componentBean)) {
					result.add(componentBean);
				}
			}
		}
		return result;
	}

	private static boolean haveAuthority(MenuComponentBean cb) {
		return cb.getPrivilegestr() == null
				|| cb.getPrivilegestr().trim().equals("")
				|| authorityList == null
				|| authorityList.contains(cb.getPrivilegestr());
	}

	public static void main(String[] args) {
		initConf("conf/default/menu.xml");
		Set<String> keySet = menuConf.keySet();
		Iterator<String> it = keySet.iterator();
		while (it.hasNext()) {
			String key = it.next();
			MenuBean value = menuConf.get(key);
			System.out.println("name:" + value.getName());
			System.out.println("i18nName" + value.getI18nName());
			List<MenuComponentBean> a = new ArrayList<MenuComponentBean>();
			a = value.getMenuList();
			if (a != null && a.size() != 0) {

				for (int i = 0; i < a.size(); i++) {
					MenuComponentBean comp = new MenuComponentBean();
					comp = a.get(i);
					System.out.println("componentName:" + comp.getName());
				}
			}
		}
	}

	public static Map<String, MenuBean> getMenuConf() {
		return menuConf;
	}

}
